package common.tool.box.web.controller.utils.markdown;

import common.tool.box.common.annotation.Log;
import common.tool.box.common.core.domain.AjaxResult;
import common.tool.box.common.core.domain.model.MarkdownFormatParam;
import common.tool.box.common.enums.BusinessType;
import common.tool.box.utils.markdown.MarkdownFormatUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/utils/markdown")
@Api(tags = "markdown")
public class MarkdownController
{
    private static final Logger log = LoggerFactory.getLogger(MarkdownController.class);

    /**
     * txt文件转markdown文本
     */
    @ApiOperation("txt文件转markdown文本")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataType = "MultipartFile", dataTypeClass = MultipartFile.class)
    })
    @PostMapping("/fileToMarkdown")
    @Log(title = "txt文件转markdown文本", businessType = BusinessType.TOOL)
    public AjaxResult markdownFileToMarkdown(MultipartFile file, MarkdownFormatParam param) throws Exception
    {
        try
        {
            String str = MarkdownFormatUtils.MultipartFileToMarkdown(file, param);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("result", str);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}
